define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'produce/index/index',
                    add_url: 'produce/index/add',
                    edit_url: 'produce/index/edit',
                    del_url: 'produce/index/del',
                    multi_url: 'produce/index/multi',
                    detail_url: 'produce/index/detail',
                    table: 'index'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                showExport:false,
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: __('ID'),operate: false,sortable: true},
                        {field: 'produce_sn', title: '排单号', sortable: false,operate: 'like'},
                        {field: 'order.order_sn', title: '订单号', sortable: false},
                        {field: 'order.customer_name', title: '客户', sortable: false},
                        {
                            field: 'finish_time',
                            title: __('要求完工期'),
                            formatter: Table.api.formatter.date,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {
                            field: 'actual_finish_time',
                            title: __('实际完工时间'),
                            formatter: Table.api.formatter.date ,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {field: 'finish_status', title: __("完工状态"), searchList: {"1":"已完成","0":"待完成","2":"逾期完成"}, custom:{"1":"green","0":"warm","2":"red"},formatter: Table.api.formatter.status},
                        {field: 'status', title: __("Status"), searchList: {"1":"正常","-1":"异常"}, custom:{"1":"green","-1":"red"},formatter: Table.api.formatter.status},
                        {
                            field: 'createtime',
                            title: __('下单时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {
                            field: 'updatetime',
                            title: __('Updatetime'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true,
                            width: 150
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            buttons: [
                                {
                                    name:'pass',//名称
                                    text:'完工',
                                    title:'完工',
                                    classname: 'btn btn-xs btn-info btn-view btn-ajax',
                                    icon: 'fa fa-check',//图标 可在添加菜单规则处 搜索图标见示例图
                                    url: 'produce/index/finish?produce_id={row.id}',//接口地址 控制器名/方法名
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.finish_status == 0){
                                            return true;//显示
                                        }else{
                                            return false;//隐藏
                                        }
                                    },
                                    refresh:true,
                                    confirm: "是否确认完工？"
                                },
                                {
                                    name: 'edit',
                                    text: __('Edit'),
                                    title: __('Edit'),
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    icon: 'fa fa-pencil',
                                    url: $.fn.bootstrapTable.defaults.extend.edit_url,
                                    visible:function(row){//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if(row.status == 0){
                                            return true;//显示
                                        }else{
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'detail',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-success',
                                    url: $.fn.bootstrapTable.defaults.extend.detail_url,
                                },
                            ],
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 打印
            $('.btn-print').click(function () {
                var ids = Table.api.selectedids(table);//获取选中列的id
                Fast.api.open("produce/index/print?id="+ids, __('打印订单'), {
                    area: ['100%', '100%']
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {

            $(document).on("fa.event.appendfieldlist", ".btn-append", function(){
                Form.events.selectpage($("#product_json"));
            });

            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("fa.event.appendfieldlist", ".btn-append", function(){
                Form.events.selectpage($("#product_json"));
            });

            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },

        }

    };
    return Controller;
});
